<?php
/**
 * 毛文科
 */
namespace app\admin\model;
class Modular{
    /**获取目前有的模块
     * @param string|null $path
     * @param array $array
     * @return array
     */
    static public function getDirList(string $path=null,array $array=[]){
        if(!$path){
            $path = '../application';
        }
        $dir =scandir($path);
        $data = [];
        foreach ($dir as $value){
            if($value!=='.'&&$value!=='..'){
                if(is_dir(rtrim($path,'/').'/'.$value)){
                    if(!in_array($value,$array)){
                        $data[] = $value;
                    }
                }
            }
        }
        return $data;
    }

    /**保存数据配置
     * @param string $filename 配置文件
     * @param array $data 数据
     * @return bool|int
     */
    static public function changeConfig($data=[],$filename='../application/admin/config/modular.php'){
        $str = '<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/21
 * Time: 13:02
 */
return ['.PHP_EOL;
        foreach ($data as $value){
            $str .='['.PHP_EOL;
            foreach ($value as $key=>$v){
                if($key==count($value)-1){
                    $str.= '"'.$key.'"=>"'.$v.'"'.PHP_EOL;
                }else{
                    $str.= '"'.$key.'"=>"'.$v.'",'.PHP_EOL;
                }
            }
            $str .='],'.PHP_EOL;
        }
        $str.='];'.PHP_EOL;
        $bool = file_put_contents($filename,$str);
        return $bool;
    }
}
